﻿#include "mainwindow.h"
#include "addclassdlg.h"
#include "addschooldlg.h"
#include "addstudentdlg.h"
#include "classdata.h"
#include "schooldata.h"
#include "studentdata.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QMessageBox>
#include <QScreen>
#include <QTableWidgetItem>
#include <QTreeWidgetItem>

MainWindow::MainWindow(QWidget* parent)
	: QMainWindow(parent)
	, ui(new Ui::MainWindow)
{
	ui->setupUi(this);
	//缩小倍数
	float minification = 0.5f;
	//获取主屏幕分辨率
	QScreen* screen = QGuiApplication::primaryScreen();
	int screenWith = screen->availableGeometry().width();
	int screenHeight = screen->availableGeometry().height();
	//根据横纵比初始化窗口大小
	int defaultWith = screenWith - screenWith * minification;
	int defaultHeight = defaultWith * screenHeight / screenWith;
	this->setFixedSize(defaultWith, defaultHeight);
	m_schoolList.clear();

	ui->tableWidget->horizontalHeader()->setDefaultAlignment(
		Qt::AlignHCenter); //表头字体居中
	ui->tableWidget->setEditTriggers(
		QAbstractItemView::NoEditTriggers); //单元格不可编辑
	ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //均分各列

	//初始化学校
	SchoolData* mySchool = new SchoolData;
	mySchool->setSchoolName(QStringLiteral("四川省绵阳中学"));

	m_schoolList.append(mySchool);

	//初始化班级
	ClassData* myClass = new ClassData;
	myClass->setClassNum(QStringLiteral("1班"));
	m_schoolList.at(0)->appendClassList(myClass);
	//初始化学生
	StudentData* myStudent = new StudentData;
	myStudent->setStuName(QStringLiteral("张三"));
	myStudent->setStuNum(QStringLiteral("1001"));
	myStudent->setStuSex(QStringLiteral("男"));
	myStudent->setStuGradeNum(QStringLiteral("二年级"));
	myStudent->setStuHeight(QStringLiteral("60"));
	m_schoolList.at(0)->getClassList().at(0)->appendStudentList(myStudent);
	//初始化树形链表
	QTreeWidgetItem* schoolItem = new QTreeWidgetItem(QStringList() << m_schoolList.at(0)->getSchoolName());
	ui->treeWidget->addTopLevelItem(schoolItem);
	QTreeWidgetItem* classItem = new QTreeWidgetItem(
		QStringList() << m_schoolList.at(0)->getClassList().at(0)->getClassNum());
	schoolItem->addChild(classItem);
	QTreeWidgetItem* studentItem = new QTreeWidgetItem(QStringList() << m_schoolList.at(0)
		->getClassList()
		.at(0)
		->getStudentList()
		.at(0)
		->getStuName());
	classItem->addChild(studentItem);

	connect(ui->treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)),
		this, SLOT(treeDoubleClicked(QTreeWidgetItem*)));

	connect(ui->treeWidget, SIGNAL(customContextMenuRequested(QPoint)), this,
		SLOT(treeRightMenu(QPoint)));
	connect(ui->addSchoolAction, &QAction::triggered, this,
		[=]() { addSchool(); });

	connect(ui->delSchoolAction, &QAction::triggered, this,
		[=]() { delSchool(); });

	connect(ui->addClassAction, &QAction::triggered, this, [=]() { addClass(); });

	connect(ui->addStudentAction, &QAction::triggered, this,
		[=]() { addStudent(); });

	connect(ui->delClassAction, &QAction::triggered, this, [=]() { delClass(); });

	connect(ui->delStudentAction, &QAction::triggered, this,
		[=]() { delStudent(); });
}

MainWindow::~MainWindow() { delete ui; }

void MainWindow::treeDoubleClicked(QTreeWidgetItem* item)
{
	item = ui->treeWidget->currentItem();
	if (item->parent() == nullptr) {
		return;
	}
	else if (item->parent() != nullptr && item->parent()->parent() == 0) {
		for (int i = ui->tableWidget->rowCount(); i > 0; i--) {
			ui->tableWidget->removeRow(0);
		}

		int schoolIndex = searchSchool(item->parent());
		QList<ClassData*> myClassList = m_schoolList.at(schoolIndex)->getClassList();

		int classIndex = searchClassInSchool(item, schoolIndex);
		QList<StudentData*> myStudentList = myClassList.at(classIndex)->getStudentList();
		for (int i = 0; i < myStudentList.count(); i++) {
			ui->tableWidget->insertRow(i);
			ui->tableWidget->setItem(
				i, 0, new QTableWidgetItem(myStudentList.at(i)->getStuName()));
			ui->tableWidget->setItem(
				i, 1, new QTableWidgetItem(myStudentList.at(i)->getStuNum()));
			ui->tableWidget->setItem(
				i, 2, new QTableWidgetItem(myStudentList.at(i)->getStuSex()));
			ui->tableWidget->setItem(
				i, 3, new QTableWidgetItem(myStudentList.at(i)->getStuGradeNum()));
			ui->tableWidget->setItem(
				i, 4, new QTableWidgetItem(myStudentList.at(i)->getStuHeight()));
			for (int j = 0; j < 5; j++) {
				ui->tableWidget->item(i, j)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
			}
		}
	}
	else {
		return;
	}
}
// 树形链表请求右键菜单响应槽函数
void MainWindow::treeRightMenu(const QPoint& pos)
{
	QTreeWidgetItem* item = ui->treeWidget->itemAt(pos);
	QMenu rightMenu(ui->treeWidget);

	if (item == nullptr) {
		rightMenu.addAction(ui->addSchoolAction);
	}
	else if (item->parent() == nullptr) {
		rightMenu.addAction(ui->delSchoolAction);
		rightMenu.addAction(ui->addClassAction);
	}
	else if (item->parent() != nullptr && item->parent()->parent() == nullptr) {
		rightMenu.addAction(ui->delClassAction);
		rightMenu.addAction(ui->addStudentAction);
	}
	else if (item->parent()->parent() != nullptr) {
		rightMenu.addAction(ui->delStudentAction);
	}
	rightMenu.exec(QCursor::pos());
}

void MainWindow::addSchool()
{
	AddSchoolDlg myAddSchoolDlg;
	myAddSchoolDlg.exec();
	if (myAddSchoolDlg.getSchoolName().isEmpty() == true) {
		return;
	}
	for (int i = 0; i < m_schoolList.count(); i++) {
		if (m_schoolList.at(i)->getSchoolName() == myAddSchoolDlg.getSchoolName()) {
			QMessageBox::information(this, QStringLiteral("提示"),
				QStringLiteral("该学校已存在，请核对后添加！"));
			return;
		}
	}
	SchoolData* mySchool = new SchoolData;
	mySchool->setSchoolName(myAddSchoolDlg.getSchoolName());
	m_schoolList.append(mySchool);
	QTreeWidgetItem* schoolUpdateItem = new QTreeWidgetItem(QStringList() << myAddSchoolDlg.getSchoolName());
	ui->treeWidget->addTopLevelItem(schoolUpdateItem);
}

void MainWindow::addClass()
{
	AddClassDlg myAddClassDlg;
	myAddClassDlg.exec();

	QTreeWidgetItem* currentClassItem = ui->treeWidget->currentItem();

	if (myAddClassDlg.getClassNum().isEmpty() == true)
		return;
	int schoolIndex = searchSchool(currentClassItem);
	QList<ClassData*> myClassList = m_schoolList.at(schoolIndex)->getClassList();
	for (int i = 0; i < myClassList.count(); i++) {
		if (myClassList.at(i)->getClassNum() == myAddClassDlg.getClassNum()) {
			QMessageBox::information(this, QStringLiteral("提示"),
				QStringLiteral("该班级已存在，请核对后添加！"));
			return;
		}
	}
	ClassData* myClass = new ClassData;
	myClass->setClassNum(myAddClassDlg.getClassNum());
	m_schoolList.at(schoolIndex)->appendClassList(myClass);
	QTreeWidgetItem* classUpdatedItem = new QTreeWidgetItem(QStringList() << myAddClassDlg.getClassNum());
	currentClassItem->addChild(classUpdatedItem);
}

void MainWindow::addStudent()
{
	AddStudentDlg myAddStudentDlg;
	myAddStudentDlg.exec();
	QTreeWidgetItem* currentStudentItem = ui->treeWidget->currentItem();
	if (myAddStudentDlg.getStuName().isEmpty() == true) {
		return;
	}
	int schoolIndex = searchSchool(currentStudentItem->parent());
	QList<ClassData*> myClassList = m_schoolList.at(schoolIndex)->getClassList();
	int classIndex = searchClassInSchool(currentStudentItem, schoolIndex);
	QList<StudentData*> myStudentList = myClassList.at(classIndex)->getStudentList();

	for (int i = 0; i < myStudentList.count(); i++) {
		if (myStudentList.at(i)->getStuName() == myAddStudentDlg.getStuName()) {
			QMessageBox::information(
				nullptr, QStringLiteral("提示"),
				QStringLiteral("该学生学号已存在，请核对后添加！"));
			return;
		}
	}
	StudentData* myStudent = new StudentData;
	myStudent->setStuName(myAddStudentDlg.getStuName());
	myStudent->setStuNum(myAddStudentDlg.getStuNum());
	myStudent->setStuSex(myAddStudentDlg.getStuSex());
	myStudent->setStuGradeNum(myAddStudentDlg.getStuGradeNum());
	myStudent->setStuHeight(myAddStudentDlg.getStuHeight());
	myClassList.at(classIndex)->appendStudentList(myStudent);
	treeDoubleClicked(currentStudentItem);
	QTreeWidgetItem* studentUpdatedItem = new QTreeWidgetItem(QStringList() << myAddStudentDlg.getStuName());
	currentStudentItem->addChild(studentUpdatedItem);
}

void MainWindow::delSchool()
{
	QTreeWidgetItem* schoolItem = ui->treeWidget->currentItem();
	int schoolIndex = searchSchool(schoolItem);
	m_schoolList.removeAt(schoolIndex);
	delete ui->treeWidget->takeTopLevelItem(ui->treeWidget->currentIndex().row());
}

void MainWindow::delClass()
{
	QTreeWidgetItem* item = ui->treeWidget->currentItem();
	int schoolIndex = searchSchool(item->parent());
	int classIndex = searchClassInSchool(item, schoolIndex);
	m_schoolList.at(schoolIndex)->removeClassList(classIndex);
	delete item->parent()->takeChild(ui->treeWidget->currentIndex().row());
}

void MainWindow::delStudent()
{
	QString stuName = ui->treeWidget->currentItem()->text(ui->treeWidget->currentIndex().row());
	//删除Tree数据

	QTreeWidgetItem* currentStudentItem = ui->treeWidget->currentItem();
	int schoolIndex = searchSchool(currentStudentItem->parent()->parent());
	QList<ClassData*> myClassList = m_schoolList.at(schoolIndex)->getClassList();
	int classIndex = searchClassInSchool(currentStudentItem->parent(), schoolIndex);

	QList<StudentData*> myStudentList = myClassList.at(classIndex)->getStudentList();
	for (int i = 0; i < myStudentList.count(); i++) {
		if (myStudentList.at(i)->getStuName() == stuName) {
			myClassList.at(classIndex)->removeStudentList(i);
		}
	}

	delete currentStudentItem->parent()->takeChild(
		ui->treeWidget->currentIndex().row());
	//删除Table数据
	ui->tableWidget->removeRow(ui->treeWidget->currentIndex().row());
	// 同步显示数据表格
	treeDoubleClicked(currentStudentItem);
}

int MainWindow::searchSchool(QTreeWidgetItem* item)
{
	int schoolIndex = 0;
	for (; schoolIndex < m_schoolList.count(); schoolIndex++) {
		if (m_schoolList.at(schoolIndex)->getSchoolName() == item->text(0))
			break;
	}
	return schoolIndex;
}

int MainWindow::searchClassInSchool(QTreeWidgetItem* item, int schoolIndex)
{
	QList<ClassData*> myClassList = m_schoolList.at(schoolIndex)->getClassList();
	int classIndex = 0;
	for (; classIndex < myClassList.count(); classIndex++) {
		if (myClassList.at(classIndex)->getClassNum() == item->text(0)) {
			break;
		}
	}
	return classIndex;
}
